unit EditCashGames;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, ComCtrls, gnugettext, umainutils,
  DB, OracleData, DBCtrls;

type
  TuFrmEditCashGames = class(TForm)
    edt_Name: TLabeledEdit;
    edt_SmallBlind: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    edt_BigBlind: TLabeledEdit;
    edt_MinIn: TLabeledEdit;
    com_RakeType: TDBLookupComboBox;
    Label1: TLabel;
    OracleDataSetRake: TOracleDataSet;
    DataSourceRake: TDataSource;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    m_recordID: string;
    m_IsClone, m_IsEdit: boolean;
    procedure PopulateRakeCombo();
  public
    NewRecordId: string;
    procedure ClearBoxes();
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string);
    { Public declarations }
  end;

var
  uFrmEditCashGames: TuFrmEditCashGames;

implementation

uses UfrmMain, uUtils;
{$R *.dfm}
{ TuFrmEditCashGames }

procedure TuFrmEditCashGames.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditCashGames.btn_SaveClick(Sender: TObject);
begin
  if m_IsClone or (not m_IsEdit) then
  begin
    NewRecordId := InsertDataIntoDB('CASH_GAMES', 'NAME,SMALL_BLIND, BIG_BLIND,MIN_IN,STATUS,RAKE_ID',
      '''' + edt_Name.Text + ''', ''' + edt_SmallBlind.Text + ''', ''' + edt_BigBlind.Text + ''', ''' + edt_MinIn.Text + ''', ''' + 'CLOSED' + ''', ''' + VarToStr(com_RakeType.KeyValue) + '''');
  end
  else
  begin
    UpdateDataToDB('CASH_GAMES', 'RAKE_ID=''' + VarToStr(com_RakeType.KeyValue) + '''' + ',MIN_IN=''' + edt_MinIn.Text + '''' + ',NAME=''' + edt_Name.Text + '''' + ', SMALL_BLIND = ''' + edt_SmallBlind.Text + ''', BIG_BLIND = ''' +
        edt_BigBlind.Text + '''', 'ID=' + m_recordID);
  end;
  ClearBoxes();
  FrmMain.GetGridData(FrmMain.stg_CashGames, SQL_CashGames, false);
  FrmMain.stg_CashGamesSelectCell(self, 0, FrmMain.stg_CashGames.Row, canselect);
  close();
end;

procedure TuFrmEditCashGames.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
  ClearBoxes();
end;

procedure TuFrmEditCashGames.PopulateRakeCombo();
begin
  OracleDataSetRake.CloseAll;
  OracleDataSetRake.Session := FrmMain.OracleSession1;
  OracleDataSetRake.SQL.Clear;
  OracleDataSetRake.SQL.Text := ('select * from rakes union select null as id, null as name from dual');
  // ---
  OracleDataSetRake.Open;
  DataSourceRake.DataSet := OracleDataSetRake;
  com_RakeType.ListSource := DataSourceRake;
  com_RakeType.KeyField := 'ID';
  com_RakeType.ListField := 'NAME';
  com_RakeType.KeyValue := null;
end;

procedure TuFrmEditCashGames.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditCashGames.FormShow(Sender: TObject);
begin
  edt_SmallBlind.SetFocus;
end;

procedure TuFrmEditCashGames.ClearBoxes();
begin
  edt_Name.Text := '';
  edt_SmallBlind.Text := '';
  edt_BigBlind.Text := '';
end;

procedure TuFrmEditCashGames.ShowForm(IsEdit, IsClone: boolean; recordID: string);
var
  Query: string;
begin
  m_IsClone := IsClone;
  m_recordID := recordID;
  m_IsEdit := IsEdit;
  OracleQuery1.Session := FrmMain.OracleSession1;
  ClearBoxes();
  PopulateRakeCombo();
  if IsEdit then
  begin
    Query := 'Select * from CASH_GAMES' + ' where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    if not OracleQuery1.Eof then
    begin
      edt_Name.Text := OracleQuery1.Field('NAME');
      edt_SmallBlind.Text := OracleQuery1.Field('SMALL_BLIND');
      edt_BigBlind.Text := OracleQuery1.Field('BIG_BLIND');
      edt_MinIn.Text := OracleQuery1.Field('MIN_IN');
      com_RakeType.KeyValue := OracleQuery1.Field('RAKE_ID');
    end;
  end;
  ShowModal();
end;

end.
